/*
<license>
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
</license>
*/
#ifndef GENERIK_READ_H
#define GENERIK_READ_H

/** \file read.h
This file implements a couple of useful functions to use when trying to deal
with binary blocks of data, such as when reading from a structured binary
file.

A lot of binary data formats are stuctured by position, not by context.
For example, a 2-byte value at offset 0x032 is defined to be a 16-bit integer
which describes the size of a block of character data.

This can be hard to handle, with juggling pointers and so forth. Which are the
kind of problems that this code tries to smooth over.
*/

#include <iomanip>

#include "tracing.h"

namespace generik
{
	/** Read interface to a block of memory. 
	Returns the current value of the 
	pointer to the memory before incrementing it by the specified amount of
	bytes.
	*/
	const char* read(const char*& pointer, int increment)
	{
		GENERIK_TRACER;
		#ifdef GENERIK_TRACING
		if (increment <=16)
		{
			// the increment is small enough to log it as a short sample
			std::stringstream ss;
			ss<<std::setbase(16);
			for (int i = 0; i <increment; ++i)
			{
				ss<<(int)pointer[i];
			}
			GENERIK_TRACE("sample: "<<ss.str());
		}
		#endif
		const char* p = pointer;
		GENERIK_TRACE_VALUE((void*)p);
		pointer += increment;
		GENERIK_TRACE_VALUE((void*)pointer);
		return p;
	}

	/** Type read interface to a block of memory. 
	Returns a cast of the current pointer to the memory as a specific type,
	before incrementing the pointer by the size of bytes used by the type.
	*/
	template <typename TYPE>
	TYPE read(const char*& pointer)
	{
		GENERIK_TRACER;
		const char* p = read(pointer, sizeof(TYPE));
		return *(TYPE*)(p); // cast *before* deref
	}
	
	/** Type read interface to a block of memory. 
	Returns a cast of the current pointer to the memory as a specific type,
	before incrementing the pointer by the specified number of bytes.
	Used to skip over unused portions in the memory block.
	*/
	template <typename TYPE>
	TYPE read(const char*& pointer, int increment)
	{
		GENERIK_TRACER;
		const char* p = read(pointer, increment);
		return *(TYPE*)(p); // cast *before* deref
	}
	
	/** String read interface to a block of memory.
	Reads length number of characters from the current pointer into a 
	string and returns it, before incrementing the pointer by the specified
	length bytes.
	*/
	/*
	std::string readstring(const char*& pointer, int length)
	{
		const char* valuep = generik::read(pointer, length);
		std::string result(valuep, length);
		return result;
	}
	*/
};

#endif







